<template>
  <div class="administratorManagement">
    <el-card>
      <div>
        <!-- <el-button @click="dialogFormVisible = true" class="btn"
          >添加管理员</el-button
        > -->
        <!-- <el-button @click="dialogFormVisibleB = true" class="btn"
          >添加部门</el-button
        > -->
      </div>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="管理员ID"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column prop="account" label="账号"> </el-table-column>
        <el-table-column prop="department" label="部门"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="warning"
              @click="changePasswordBtn(scope.row)"
              >修改密码</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="getList"
      />
      <!-- 添加管理员 -->
      <el-dialog
        title="添加管理员"
        :visible.sync="dialogFormVisible"
        width="30%"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item label="姓名" prop="name" :label-width="formLabelWidth">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>

          <el-form-item
            label="账号"
            prop="account"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleForm.account"></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            prop="password"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>

          <el-form-item
            label="部门"
            prop="resource"
            :label-width="formLabelWidth"
          >
            <el-radio-group v-model="ruleForm.resource">
              <el-radio label="开发部"></el-radio>
              <el-radio label="运营部"></el-radio>
              <el-radio label="测试部"></el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="adminAddBtn">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 添加部门 -->
      <el-dialog
        title="添加部门"
        :visible.sync="dialogFormVisibleB"
        width="30%"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item
            label="部门名称"
            prop="name"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>

          <el-form-item
            label="角色1"
            prop="account"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleForm.account"></el-input>
          </el-form-item>

          <el-form-item
            label="查看权限"
            prop="resource"
            :label-width="formLabelWidth"
          >
            <el-radio-group v-model="ruleForm.resource">
              <el-radio label="开发部"></el-radio>
              <el-radio label="运营部"></el-radio>
              <el-radio label="测试部"></el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item
            label="角色2"
            prop="password"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>

          <el-form-item
            label="操作权限"
            prop="resource"
            :label-width="formLabelWidth"
          >
            <el-radio-group v-model="ruleForm.resource">
              <el-radio label="开发部"></el-radio>
              <el-radio label="运营部"></el-radio>
              <el-radio label="测试部"></el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleB = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisibleB = false"
            >确 定</el-button
          >
        </div>
      </el-dialog>

      <!-- 编辑管理员 -->
      <el-dialog
        title="编辑管理员"
        :visible.sync="dialogFormVisibleEdit"
        width="30%"
      >
        <el-form :model="ruleFormEdit" class="demo-ruleForm">
          <el-form-item label="姓名" prop="name" :label-width="formLabelWidth">
            <el-input v-model="ruleFormEdit.name"></el-input>
          </el-form-item>

          <el-form-item
            label="账号"
            prop="account"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleFormEdit.account"></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            prop="password"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleFormEdit.password"></el-input>
          </el-form-item>

          <el-form-item
            label="部门"
            prop="resource"
            :label-width="formLabelWidth"
          >
            <el-radio-group v-model="ruleFormEdit.resource">
              <el-radio label="开发部"></el-radio>
              <el-radio label="运营部"></el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisibleEdit = false"
            >确 定</el-button
          >
        </div>
      </el-dialog>

      <!-- 修改密码 -->
      <el-dialog
        title="修改密码"
        :visible.sync="dialogchangePassword"
        width="30%"
      >
        <el-form :model="ruleFormchangePassword" class="demo-ruleForm">
          <el-form-item
            label="旧密码"
            prop="password"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="ruleFormchangePassword.oldPwd"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="新密码"
            prop="password"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="ruleFormchangePassword.password"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="确认密码"
            prop="password"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="ruleFormchangePassword.confirmPwd"
              type="password"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogchangePassword = false">取 消</el-button>
          <el-button type="primary" @click="confirmPasswordBtn"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // 分页
import {
  adminList,
  adminDel,
  adminAdd,
  adminChangePwd,
} from "@/api/SystemManagement";

export default {
  components: { Pagination },
  inject: ["reload"],
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisibleB: false,
      dialogFormVisibleEdit: false,
      dialogchangePassword: false,
      ruleForm: {
        name: "",
        account: "",
        password: "",
        resource: "",
      },
      ruleFormEdit: {
        name: "",
        account: "",
        password: "",
        resource: "",
      },
      ruleFormchangePassword: {
        id: "",
        oldPwd: "",
        password: "",
        confirmPwd: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        account: [
          { required: true, message: "请输入账号", trigger: "blur" },
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        resource: [{ required: true, message: "", trigger: "blur" }],
      },
      formLabelWidth: "80px",
      total: 0,
      page: 1, //页码
      pageSize: 10, //每页条数
    };
  },
  methods: {
    //列表
    adminList() {
      adminList({
        page: this.page,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.total = parseInt(res.data.count);
        }
      });
    },
    //分页
    getList() {
      adminList({
        page: this.page,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.total = parseInt(res.data.count);
        }
      });
    },
    //添加管理员
    adminAddBtn() {
      adminAdd({
        nickname: this.ruleForm.name,
        account: this.ruleForm.account,
        password: this.ruleForm.password,
        resource: this.ruleForm.resource,
      }).then((res) => {
        if (res.code == 0) {
          this.$message({
            type: "success",
            message: "添加成功",
          });
          this.reload();
          this.dialogFormVisible = false;
        }
      });
    },
    handleEdit(row) {
      // console.log(row);
      this.ruleFormEdit.name = row.createName;
      this.ruleFormEdit.account = row.account;
      // this.ruleFormEdit.name = row.name;
      // this.ruleFormEdit.name = row.name;
      this.dialogFormVisibleEdit = true;
    },
    //删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除该账号, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          adminDel({
            id: row.id,
            del: 1,
          }).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.reload();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改密码
    changePasswordBtn(row) {
      this.dialogchangePassword = true;
      this.ruleFormchangePassword.id = row.id;
    },
    confirmPasswordBtn() {
      if (
        this.ruleFormchangePassword.password ==
        this.ruleFormchangePassword.confirmPwd
      ) {
        adminChangePwd({
          id: this.ruleFormchangePassword.id,
          oldPwd: this.ruleFormchangePassword.oldPwd,
          password: this.ruleFormchangePassword.password,
          confirmPwd: this.ruleFormchangePassword.confirmPwd,
        }).then((res) => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: "修改密码成功",
            });
            this.reload();
          }
        });
      } else {
        this.$message({
          type: "error",
          message: "新密码与确认密码不一致",
        });
      }
    },
  },
  created() {
    this.adminList();
  },
};
</script>

<style lang="less" scoped>
.administratorManagement {
  height: 100%;
  margin: 20px;
  .btn {
    margin-bottom: 20px;
  }
  .el-dialog {
    .el-input {
      width: 300px;
    }
  }
}
</style>